<?php
namespace Seller\Controller;
use Think\Controller;

class PaymentController extends CommonController
{
    public $shop_id;

    public function _initialize()
    {
        $param = array("pay_install","pay_edit","pay_uninstall");
        parent::_initialize($param);
        $this->shop_id = session("shop_id");
    }

    // 支付方式
    public function paylist()
    {
        $payments = M("payment")->select();
        foreach($payments as $pk => $pv){
            $pconf = M("payment_conf")
                ->where("p_id = %d AND shop_id = %d" ,array($pv["p_id"] ,$this->shop_id))
                ->find();
            $payments[$pk]['p_fee'] = $pconf['p_fee'];
            $payments[$pk]['installed'] = $pconf['installed'];
        }
        $this->assign("payments" ,$payments);
        $this->display();
    }

    // 支付方式安装
    public function pay_install()
    {
        if(IS_POST)
        {
            $p_id = to_int(I('post.p_id'));
            $config = I('post.');
            $payment = M("payment")->where("p_id = $p_id")->find();
            if(!$payment){
                $this->error("请求有误" ,U("Payment/paylist") ,1);
            }
            $pay_conf = array();
            $conf_code = unserialize($payment['p_params']);
            foreach($conf_code as $conf_k => $conf_v)
            {
                $conf_name = strtolower($conf_v['name']);
                foreach($config as $k => $v){
                    if($k == $conf_name){
                        $conf_code[$conf_k]["value"] = '';
                        $pay_conf[$conf_name] = $v;
                    }
                }
            }
            $save_data["p_params"] = serialize($conf_code);
            $save_data["p_name"] = trim(I('post.p_name'));
            $save_data["p_desc"] = trim(I('post.p_desc'));
            $conf_data["installed"] = 1;
            $conf_data["pay_conf"] = serialize($pay_conf);
            $conf_data["p_id"] = $p_id;
            $conf_data["shop_id"] = $this->shop_id;
            $conf_data["p_fee"] = trim(I('post.p_fee'));
            M("payment")->where("p_id = $p_id")->save( $save_data);
            $pay_isset = M("payment_conf")->where("p_id = %d AND shop_id = %d",array($p_id ,$this->shop_id))->find();
            if(!$pay_isset){
                M("payment_conf")->add( $conf_data);
            }else{
                M("payment_conf")->where("p_id = %d AND shop_id = %d",array($p_id ,$this->shop_id))->save( array(
                    "pay_conf" => serialize($pay_conf),
                    "installed" => 1,
                    "p_fee" => trim(I('post.p_fee'))
                ));
            }
            $this->success("操作成功" ,U("Payment/paylist") ,1);
        }else{
            $p_id = to_int(I("get.id"));
            $payment = M("payment")->where("p_id = $p_id")->find();
            if(!$payment){
                $this->error("请求有误" ,U("Payment/paylist") ,1);
            }
            $payment['p_fee'] =  0;
            $this->assign("payment_html" ,build_payment_html( $payment['p_params'] ));
            $this->assign("payment_id" ,$p_id);
            $this->assign("payment" ,$payment);
            $this->display();
        }
    }

    // 支付方式编辑
    public function pay_edit()
    {
        $p_id = to_int(I("get.id"));
        $payment = M("payment")->where("p_id = $p_id")->find();
        if(!$payment){
            $this->error("请求有误" ,U("Payment/paylist") ,1);
        }
        $pconf = M("payment_conf")->where("p_id = %d AND shop_id = %d" ,array($p_id ,$this->shop_id))->find();
        $payment['p_fee'] = $pconf['p_fee'] ? $pconf['p_fee']: 0;
        $payment['pay_conf'] = $pconf['pay_conf'] ? $pconf['pay_conf']: '';
        $this->assign("payment_html" ,build_payment_html( $payment['p_params'] ,$payment['pay_conf'] ));
        $this->assign("payment_id" ,$p_id);
        $this->assign("payment" ,$payment);
        $this->display("pay_install");
    }

    // 支付方式编辑
    public function pay_uninstall()
    {

        $p_id = to_int(I("get.id"));
        $pay = M("payment_conf")->where("p_id = %d AND installed = 1 AND shop_id = %d" ,array($p_id, $this->shop_id))->find();
        if(!$pay){
            $this->error("请求有误" ,U("Payment/paylist") ,1);
        }
        $payment = M("payment")->where("p_id = %d" ,array($p_id ))->find();
        M("payment_conf")->where("p_id = %d AND shop_id = %d" ,array($p_id, $this->shop_id))->save(array("installed" => 0, "p_fee"=> 0));
        $conf_code = unserialize($payment['p_params']);
        foreach($conf_code as $conf_k => $conf_v)
        {
            $conf_code[$conf_k]["value"] = "";
        }
        $pay_conf = unserialize($payment['p_conf']);
        foreach($pay_conf as $pay_k => $pay_v)
        {
            $pay_conf[$pay_k] = "";
        }
        $data_save["pay_conf"] = serialize($pay_conf);
        M("payment_conf")->where("p_id = $p_id")->save( $data_save);
        $this->success("卸载完成" ,U("Payment/paylist") ,1);
    }
}